package com.katian.metro;

import java.io.*;

/**
 * User: csavard
 * Date: 13-11-05
 */
public class MetroUtil {

    public static void mergeFiles(OutputStream out, InputStream... filesInputs) throws IOException {
        Writer writer = new OutputStreamWriter(out);

        try {
            if (filesInputs != null && filesInputs.length > 0) {
                BufferedReader reader = new BufferedReader(new InputStreamReader(filesInputs[0]));
                mergeFiles(reader, writer);

                if (filesInputs.length > 1) {
                    for (int i = 1; i < filesInputs.length; i++) {
                        reader = new BufferedReader(new InputStreamReader(filesInputs[i]));

                        //on skip la premiere ligne
                        reader.readLine();

                        mergeFiles(reader, writer);
                    }
                }
            }
        } finally {
            writer.close();
        }
    }

    private static void mergeFiles(BufferedReader input, Writer out) throws IOException {
        try {
            //on ajoute dans le output les nouvelles lignes
            while (input.ready()) {
                String line = input.readLine() + "\n";
                out.append(line);
            }
        } finally {
            input.close();
            out.flush();
        }
    }
}
